
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>

/* Private function prototypes -----------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern pFunction Jump_To_Application;
extern uint32_t JumpAddress;

/* Private functions ---------------------------------------------------------*/
void UART_Configuration(void);
void UART_DEBUG_Configuration(void);
void LED_Configuration(void);

#ifdef __GNUC__
  /* With GCC/RAISONANCE, Large printf (option LD Linker->Libraries->Large printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/***********************************************************
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	uint32_t for_tmp;
	uint32_t Count_tmp;
	uint8_t Check_sum,Check_sum_tmp;
	
	/* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
	if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000){

		Count_tmp = *(__IO uint32_t*)APPINFO_ADDRESS;
		Check_sum_tmp = *(__IO uint32_t*)(APPINFO_ADDRESS+4);
		Check_sum = 0;
		
		if(Count_tmp < 0x7BF00) {

			for(for_tmp=0;for_tmp<Count_tmp;for_tmp++){
				 Check_sum += *(__IO uint8_t*)(APPLICATION_ADDRESS+for_tmp);
			}

			if(Check_sum == Check_sum_tmp){
				/* Jump to user application */
				JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
				Jump_To_Application = (pFunction) JumpAddress;
				/* Initialize user application's Stack Pointer */
				__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
				
				Jump_To_Application();
			}

		}
	}
	
  /* Unlock the Flash Program Erase controller */
  FLASH_If_Init();
	
	FLASH_If_DisableWriteProtection();
		
	LED_Configuration();
	UART_Configuration();
	
#ifdef  DEBUG_UART_ON
	UART_DEBUG_Configuration();
#endif
	
	Prog_function();

	while(1);
}

/***********************************************************
  * @brief  LED_Configuration
  * @param  None
  * @retval None
  */
void LED_Configuration(void)
{
#ifdef LED_Enable
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

	RCC_AHB1PeriphClockCmd(LED1_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED1_PIN;
	GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED2_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED2_PIN;
	GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED3_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED3_PIN;
	GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED4_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED4_PIN;
	GPIO_Init(LED4_GPIO_PORT, &GPIO_InitStructure);
#endif
}

/***********************************************************
  * @brief  UART_Configuration
  * @param  None
  * @retval None
  */
void UART_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
//	NVIC_InitTypeDef NVIC_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* Enable GPIO clock */
	RCC_AHB1PeriphClockCmd(IAP_TX_GPIO_CLK, ENABLE);
	
	/* Enable UART clock */
#ifdef IAP_COM_USART1
	RCC_APB2PeriphClockCmd(IAP_CLK, ENABLE);
#else
	RCC_APB1PeriphClockCmd(IAP_CLK, ENABLE);
#endif
	
	/* Connect Tx*/
	GPIO_PinAFConfig(IAP_TX_GPIO_PORT, IAP_TX_SOURCE, IAP_TX_AF);
	/* Connect Rx*/
	GPIO_PinAFConfig(IAP_RX_GPIO_PORT, IAP_RX_SOURCE, IAP_RX_AF);

	/* Configure IAP_COM Tx as alternate function  */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	
	GPIO_InitStructure.GPIO_Pin = IAP_TX_PIN;
	GPIO_Init(IAP_TX_GPIO_PORT, &GPIO_InitStructure);
	/* Configure USART Rx as alternate function  */
	GPIO_InitStructure.GPIO_Pin = IAP_RX_PIN;
	GPIO_Init(IAP_RX_GPIO_PORT, &GPIO_InitStructure);

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(IAP_COM, &USART_InitStructure);
}

/***********************************************************
  * @brief  UART_Configuration
  * @param  None
  * @retval None
  */
void UART_DEBUG_Configuration(void)
{
#ifdef  DEBUG_UART_ON
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* Enable GPIO clock */
	RCC_AHB1PeriphClockCmd(Debug_TX_GPIO_CLK, ENABLE);
	
	/* Enable UART clock */
	RCC_APB1PeriphClockCmd(Debug_CLK, ENABLE);
	
	/* Connect Tx*/
	GPIO_PinAFConfig(Debug_TX_GPIO_PORT, Debug_TX_SOURCE, Debug_TX_AF);
	/* Connect Rx*/
	GPIO_PinAFConfig(Debug_RX_GPIO_PORT, Debug_RX_SOURCE, Debug_RX_AF);

	/* Configure Debug_COM Tx as alternate function  */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	
	GPIO_InitStructure.GPIO_Pin = Debug_TX_PIN;
	GPIO_Init(Debug_TX_GPIO_PORT, &GPIO_InitStructure);
	/* Configure USART Rx as alternate function  */
	GPIO_InitStructure.GPIO_Pin = Debug_RX_PIN;
	GPIO_Init(Debug_RX_GPIO_PORT, &GPIO_InitStructure);

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(Debug_COM, &USART_InitStructure);
#endif
}


/****************************************************
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
	
  /* Loop until the end of transmission */
	
#ifdef  DEBUG_UART_ON
//	while (USART_GetFlagStatus(Debug_COM, USART_FLAG_TC) == RESET);
//  USART_SendData(Debug_COM, (uint8_t) ch);
#endif	
  return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

